﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class Keyword : QT.Windows.Shell.GlassForm
    {
        #region Form

        public Keyword()
        {
            InitializeComponent();
            InitComboBox(0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.pSetting.Location = new Point((this.ClientSize.Width - this.pSetting.Width) / 2, (this.ClientSize.Height - this.pSetting.Height) / 2);
        }

        #endregion

        #region Event

        #region Button

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的记录");
                return;
            }
            if (this.dgvList.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一条记录");
                return;
            }

            this.rbtnKeywords.Text = this.dgvList.SelectedRows[0].Cells[1].Value.ToString();
            this.pSetting.Tag = this.dgvList.SelectedRows[0].Cells[2].Value.ToString();
            this.pSetting.Visible = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(this.pSetting.Tag==null||string.IsNullOrEmpty(this.pSetting.Tag.ToString()))
            {
                MessageBox.Show("内部错误：找不到Guid");
                return;
            }

            int rval = QT.SEO.Keyword.Update(this.pSetting.Tag.ToString(),this.rbtnKeywords.Text);
            switch(rval)
            {
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                case 1:
                    MessageBox.Show("操作成功");
                    cbbGroup_SelectedIndexChanged(sender,e);
                    btnClose_Click(sender,e);
                    break;
                default:
                    MessageBox.Show("内部错误，错误码："+rval);
                    break;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.pSetting.Visible = false;
            this.rbtnKeywords.Text = "";
            this.pSetting.Tag = "";
        }

        #endregion

        #region ComboBox

        private void cbbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = QT.SEO.URL.GetAll(this.cbbGroup.SelectedValue.ToString());
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.Columns[0].DataPropertyName = "url";
            this.dgvList.Columns[1].DataPropertyName = "keywords";
            this.dgvList.Columns[2].DataPropertyName = "guid";
            this.dgvList.DataSource = dt;
            this.dgvList.AutoResizeColumns();
        }

        #endregion

        #region DataGridView

        private void dgvUrlList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("网址总数：{0}", this.dgvList.RowCount);
        }

        private void dgvUrlList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            this.tsslPostion.Text = string.Format("     行：{0}     列{1}", e.RowIndex + 1, e.ColumnIndex + 1);
        }

        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count != 0) return;

            btnSetting_Click(sender, e);
        }

        #endregion

        #region PictureBox

        private void pbAdd_Click(object sender, EventArgs e)
        {
            KeywordList myList = new KeywordList();
            myList.ShowDialog();
            this.rbtnKeywords.Text = myList.myKeywords;
            myList.Dispose();
        }

        #endregion

        #endregion

        #region Method

        public void InitComboBox(int index)
        {
            DataTable dt = QT.SEO.Group.GetAll();
            this.cbbGroup.ValueMember = dt.Columns[0].ToString();
            this.cbbGroup.DisplayMember = dt.Columns[1].ToString();
            this.cbbGroup.DataSource = dt;

            this.cbbGroup.SelectedIndex = index >= this.cbbGroup.Items.Count ? 0 : index;
        }

        #endregion
    }
}
